<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
    <link rel="stylesheet" href="lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layuimini-main">
    <div class="layui-form layuimini-form" lay-filter="formTest">
        <!--    记录id,方便修改-->
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户姓名：</label>
            <div class="layui-input-block">
                <input type="text" name="name" readonly lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">性别：</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="1" title="男" readonly checked="">
                <input type="radio" name="sex" readonly value="2" title="女">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">手机号码：</label>
            <div class="layui-input-block">
                <input type="number" name="phone" readonly lay-verify="required" lay-reqtext="手机不能为空" placeholder="请输入手机" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">申请挂号时间：</label>
            <div class="layui-input-block">
                <input id="date" name="registertime" readonly placeholder="请选择预约时间" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">症状：</label>
            <div class="layui-input-block">
                <input type="text" name="symptom" lay-verify="required" lay-reqtext="病人症状" placeholder="病人症状" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="buy">查询药品</button>
            </div>
        </div>
    </div>

    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

    <script type="text/html" id="currentTableBar">
        <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="buyone">购买</a>
    </script>
</div>



<script src="lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    $(function (){
        var json=localStorage.getItem("diagnose");
        var u =JSON.parse(json);
        var effect;
        layui.use(['form','laydate','table'], function () {
            var form = layui.form,
                layer = layui.layer,
                laydate=layui.laydate,
                table=layui.table,
                $ = layui.$;
                //渲染日期框
                laydate.render({
                    elem: '#date' //指定元素
                });
                //渲染病人信息
                form.val("formTest", u);
                table.render({
                    elem: '#currentTableId',
                    url: '/api/medicine/effect.do',
                    toolbar: '#toolbarDemo',
                    defaultToolbar: ['filter', 'exports', 'print', {
                        title: '提示',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    cols: [[
                        // {type: "checkbox", width: 50},
                        // {type: "radio",title:'1', width: 50},
                        // {type: "radio",title:'2', width: 50},
                        {field: 'id', title: 'ID', sort: true},
                        {field: 'name',  title: '药品名'},
                        {field: 'effect',  title: '药效'},
                        {field: 'stock', title: '库存', sort: true},
                        {field: 'sell', title: '价格', sort: true},
                        {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                    ]],
                    limits: [10, 15, 20, 25, 50, 100],
                    limit: 15,
                    page: true,
                    skin: 'line'
                });
                laydate.render({
                    elem: '#date' //指定元素
                });

                // 监听搜索操作

                form.on('submit(buy)', function (data) {
                    var result = JSON.stringify(data.field);
                    var effects = {
                        'effects':data.field.symptom+''
                    }
                    effect = data.field.symptom;
                    //执行搜索重载
                    table.reload('currentTableId', {
                        page: {
                            curr: 1
                        }
                        , where: effects
                    }, 'data');

                    return false;
                });

                /**
                 * toolbar监听事件
                 */
                table.on('toolbar(currentTableFilter)', function (obj) {
                    if (obj.event === 'add') {  // 监听添加操作
                        var index = layer.open({
                            title: '添加部门',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: 'useradd.html',
                        });
                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                    } else if (obj.event === 'delete') {  // 监听删除操作
                        var checkStatus = table.checkStatus('currentTableId')
                            , data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                    }else if (obj.event === 'end') {  // 监听删除操作
                        layer.msg('诊断结束！');
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }
                });

                //监听表格复选框选择
                table.on('checkbox(currentTableFilter)', function (obj) {
                    console.log(obj)
                });

                table.on('tool(currentTableFilter)', function (obj) {
                    var data = obj.data;
                    console.log(effect);
                    var seedoc ={
                        'uid':u.id+'',
                        'empid':u.depid+'',
                        'registertime':u.registertime+'',
                        'symptom':effect+'',
                        'medid':data.id+'',
                        //医生诊断用1表示，只能诊断用2表示
                        'stype':1,
                        'change':data.price+''
                    }

                    if (obj.event === 'buyone') {
                        if(effect==''){
                            layer.msg('症状不能为空');
                        }else {
                            //添加用户诊断记录
                            $.get('/api/user/updatesurplus.do',seedoc,function (res){
                                if(res.code==200){
                                    // console.log('-------');
                                    layer.msg("购买成功！");
                                }else {
                                    layer.msg(res.msg);
                                    // 关闭弹出层
                                    parent.layui.table.reload('currentTableId');
                                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(iframeIndex);
                                }
                                // // 关闭弹出层
                                // var iframeIndex = parent.layer.getFrameIndex(window.name);
                                // parent.layer.close(iframeIndex);
                            })
                        }

                        // layui.use('layer', function(){
                        //     var layer = layui.layer;
                        //
                        //     layer.msg('请输入购买数量');
                        // });

                    }
                });

        });
    })

</script>
</body>
</html>